package com.example.demo.service.bis.impl;

import com.example.demo.bean.bis.Sign;
import com.example.demo.bean.dto.SignDay;
import com.example.demo.bean.dto.SignPlus;
import com.example.demo.repository.SignMapper;
import com.example.demo.service.bis.SignService;
import com.example.demo.service.exception.ErrorCode;
import com.example.demo.service.exception.ServiceException;
import com.example.demo.utils.ConstantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzz
 * @Title: SignServiceImpl
 * @ProjectName test
 * @Description: TODO(签到)
 * @date 2018/7/12
 */
@Service
@Transactional
public class SignServiceImpl implements SignService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SignServiceImpl.class);


    @Autowired private SignMapper signMapper;

    @Value("${IMG.URL.SUFIX}")
    private String imgUrl;

    @Override
    public Sign findByDayAndUid(String date, Integer id) {
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(id)){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        Sign sign =  signMapper.findByDayAndUid(date,id);
        if(!StringUtils.isEmpty(sign)){
            System.out.println(sign.getImgMinUrl());
            if (!StringUtils.isEmpty(sign.getImgMinUrl())){
                sign.setImgMinUrl(imgUrl+sign.getImgMinUrl());
            }
            if (!StringUtils.isEmpty(sign.getImgUrl())){
                sign.setImgUrl(imgUrl+sign.getImgUrl());
            }
            /*if(sign.getAskForLeave()==ConstantUtils.FAILURE){
                sign =  signMapper.findUserStateByDayAndUid(date,id);
            }*/
        }
        return sign;
    }

    @Override
    public SignPlus findByUidAndDate(String date, Integer id ) {
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(id)){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        return signMapper.findByUid(date,date.length(),id);
    }

    @Override
    public List<SignPlus> findByDeptDate(String date, Integer id,String flag) {
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(id)){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        List<SignPlus> signPlusList = null;
        if(ConstantUtils.INTEGER_TEN.equals(date.length())){
            signPlusList = signMapper.findByDayDept(date);
            signPlusList.add(ConstantUtils.SUCCEED,signPlusList.get(signPlusList.size()-ConstantUtils.FAILURE));
            signPlusList.remove(signPlusList.size()-ConstantUtils.FAILURE);
            signPlusList.get(ConstantUtils.SUCCEED).setName("全部");
        } else if (!StringUtils.isEmpty(flag)){
            signPlusList = signMapper.findByDeptDetails(date,date.length(),id);
        } else {
            signPlusList = signMapper.findByDept(date,date.length(),id);
        }
        return signPlusList;
    }

    @Override
    public Map<String,Object> findBySignDetils(String date,Integer id,Integer deptId) {

        return getSignList(signMapper.findByDayDetails(date,id,deptId));
    }

    @Override
    public List<SignPlus> findByDay( String id,String date) {
        return   signMapper.findByDay(id,date);
    }

    @Override
    public SignPlus findByDeptidAndTime(Integer deptid, String date) {
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(deptid)){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        if(date.length()==ConstantUtils.INTEGER_TEN){
            return signMapper.findByDeptidAndDay(deptid,date);
        }else{
            return signMapper.findByDeptidAndTime(deptid,date,date.length());
        }
    }

    @Override
    public List<SignPlus> findByDeptidAndTimeDetails(Integer deptid, String date) {
        if(StringUtils.isEmpty(date) || StringUtils.isEmpty(deptid)){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        List<SignPlus> signPlus = new ArrayList<SignPlus>();
        /*if(date.length()==ConstantUtils.INTEGER_TEN){
            signPlus =  signMapper.findByDeptidAndDayDetails(deptid,date);
            if (!StringUtils.isEmpty(signPlus) && signPlus.size()>=6){
                if(!"无".equals(signPlus.get(ConstantUtils.INTEGER_SIX).getNames())){
                    for (int i =ConstantUtils.FAILURE ;i<=ConstantUtils.INTEGER_TWO;i++){
                        if(!"无".equals(signPlus.get(i).getNames())){
                            signPlus.get(i).setNames(signPlus.get(i).getNames()+","+signPlus.get(ConstantUtils.INTEGER_SIX).getNames());
                        }else {
                            signPlus.get(i).setNames(signPlus.get(ConstantUtils.INTEGER_SIX).getNames());
                        }
                    }
                    signPlus.remove(signPlus.size()-ConstantUtils.FAILURE);
                }
            }
            return signPlus;
        }else{*/
            signPlus = signMapper.findByDeptidAndTimeDetails(deptid,date,date.length());
            return  signPlus;
        //}
    }

    private Map<String,Object> getSignList(List<SignDay> list){

        for (int i=ConstantUtils.SUCCEED;i<list.size();i++){
            if (!ConstantUtils.STRING_NOTHING.equals(list.get(i).getImgMinUrl())){
                list.get(i).setImgMinUrl(imgUrl+list.get(i).getImgMinUrl());
            }
            if (!ConstantUtils.STRING_NOTHING.equals(list.get(i).getImgUrl())){
                list.get(i).setImgUrl(imgUrl+list.get(i).getImgUrl());
            }

        }

        Map<String,Object> map = new HashMap<>();
        if (StringUtils.isEmpty(list) && list.size()<=ConstantUtils.SUCCEED){
            map.put("normal","");
            map.put("beLate","");
            map.put("leaveEarly","");
            map.put("absenteeism","");
            map.put("absence","");
            map.put("askForLeave","");
        }else{
            List<SignDay> normal = new ArrayList<>();
            List<SignDay> beLate = new ArrayList<>();
            List<SignDay> leaveEarly = new ArrayList<>();
            List<SignDay> absenteeism = new ArrayList<>();
            List<SignDay> absence = new ArrayList<>();
            List<SignDay> askForLeave = new ArrayList<>();
            for(int i=ConstantUtils.SUCCEED;i<list.size();i++){
                switch (list.get(i).getState()){
                    case ConstantUtils.STRING_TWO:
                        normal.add(list.get(i));break;
                    case ConstantUtils.STRING_THREE:
                        beLate.add(list.get(i));break;
                    case ConstantUtils.STRING_FOUR:
                        leaveEarly.add(list.get(i));break;
                    case ConstantUtils.STRING_FIVE:
                        absenteeism.add(list.get(i));break;
                    case ConstantUtils.STRING_SIX:
                        absence.add(list.get(i));break;
                    case ConstantUtils.STRING_SEVEN:
                        askForLeave.add(list.get(i));break;
                    case ConstantUtils.STRING_EIGHT:
                        list.get(i).setState(ConstantUtils.STRING_THREE);
                        beLate.add(list.get(i));
                        SignDay signDay =  new SignDay();
                        signDay.setName(list.get(i).getName());
                        signDay.setState(ConstantUtils.STRING_FOUR);
                        signDay.setImgMinUrl(list.get(i).getImgMinUrl());
                        signDay.setImgUrl(list.get(i).getImgUrl());
                        signDay.setSignIn(list.get(i).getSignIn());
                        signDay.setSignOut(list.get(i).getSignOut());
                        signDay.setDeptName(list.get(i).getDeptName());
                        leaveEarly.add(signDay);break;
                }
            }
            if(normal.size()!=ConstantUtils.SUCCEED){
                map.put("normal",normal);
            }else{
                map.put("normal","");
            }

            if(beLate.size()!=ConstantUtils.SUCCEED){
                map.put("beLate",beLate);
            }else{
                map.put("beLate","");
            }

            if(leaveEarly.size()!=ConstantUtils.SUCCEED){
                map.put("leaveEarly",leaveEarly);
            }else{
                map.put("leaveEarly","");
            }


            if(absence.size()!=ConstantUtils.SUCCEED){
                map.put("absence",absence);
            }else{
                map.put("absence","");
            }

            if(absenteeism.size()!=ConstantUtils.SUCCEED){
                map.put("absenteeism",absenteeism);
            }else{
                map.put("absenteeism","");
            }

            if(askForLeave.size()!=ConstantUtils.SUCCEED){
                map.put("askForLeave",askForLeave);
            }else{
                map.put("askForLeave","");
            }


        }

        return map;
    }
}
